﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace CFScript.Tasks
{
    class Start: Task
    {
        public Start()
        {
            this.name = "<start>";
            this.parameterNames = new string[] {"path","args"};
            this.parameterExpandable = new bool[] { true, true};
            this.parameterRequired = new bool[] {true, false};
            this.parameters = new Dictionary<string, Parameter>();
            /*
             * System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo(_targetAppPath, Globales.Instancia.TipoComunicacion.ToString() + "," + p.Id.ToString());
             * System.Diagnostics.Process.Start(proc);
             */

            Debug.Assert(this.parameterNames.Length == this.parameterRequired.Length &&
                         this.parameterRequired.Length == this.parameterRequired.Length);
        }

        public override InstructionResult Execute(CFScript context)
        {
            InstructionResult result = new InstructionResult();
            result.ExecutedInstruction = this;

            string path = this.parameters["path"].GetValue(context);
            string args = this.parameters.ContainsKey("args")?this.parameters["args"].GetValue(context):null;

            try
            {
                ProcessStartInfo psi = new ProcessStartInfo(path, args); ;
                Process proc = Process.Start(psi);
                result.Code = ResultCode.OK;
            }
            catch (Exception ex)
            {
                result.Code = ResultCode.ERROR;
                result.Message = ex.Message;
            }
            
            result.BreakExecution = result.Code == ResultCode.ERROR && this.options.OnError == OnErrorAction.BREAK;

            return result;
        }
    }
}
